package com.lex.testcontect;

import android.content.Context;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;


import com.lex.activitytext.R;

import java.util.ArrayList;
import java.util.List;



public class TestContactActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_contact);
    }

    public void chooseContact(View view){
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent,100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode != 100 || resultCode != RESULT_OK) return;
        Uri contentUri = data.getData();
        Cursor cursor = getContentResolver().query(contentUri,null,null,null,null);
        if(cursor != null){
            cursor.moveToFirst();
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//            getPhoneNumbers(this,contactId);
            List<String> phoneNums = getContactInfo(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,contactId,
                    ContactsContract.CommonDataKinds.Phone.NUMBER);
            List<String> emails = getContactInfo(this, ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID,contactId,
                    ContactsContract.CommonDataKinds.Email.DATA);
            /*查找联系人地址*/
            String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            String[] addrWhereParams = new String[]{contactId,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
            Cursor addrCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);
            while(addrCur.moveToNext()) {
                String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            }
            getInfo(contactId);
            Log.d("Sam","");
        }
    }

    public void getInfo(String contactId){
        Cursor contactMethods = getContentResolver().query(Contacts.ContactMethods.CONTENT_URI, null,Contacts.ContactMethods.PERSON_ID + " = "+contactId,null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
        String result = "";
        while (contactMethods.moveToNext()) {
            int myKind = contactMethods.getInt(contactMethods.getColumnIndex(Contacts.ContactMethods.KIND));
            if (Contacts.KIND_EMAIL == myKind) {
                result = result + "\nemail::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA));
            }else if(Contacts.KIND_IM == myKind){
                result = result + "\nim::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA)) ;
            }else if(Contacts.KIND_ORGANIZATION == myKind){
                result = result + "\norg::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA)) ;
            }else if(Contacts.KIND_PHONE == myKind){
                result = result + "\nphone::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA));
            }else if(Contacts.KIND_POSTAL == myKind){
                result = result + "\naddress::"+contactMethods.getString(contactMethods.getColumnIndex(Contacts.ContactMethods.DATA));
            }
        }
        Log.d("SamLog",result);
    }

    public List<String> getPhoneNumbers(Context context,String contactId){
        Cursor phoneCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "+contactId,null,null);
        List<String> phoneNums = new ArrayList<>();
        while(phoneCursor != null && phoneCursor.moveToNext()){
            String phoneNum = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneNums.add(phoneNum);
        }
        return phoneNums;
    }
    public List<String> getContactInfo(Context context,Uri contentUri, String contactIdKey,String contactId,String keyToGetData){
        Cursor phoneCursor = context.getContentResolver().query(contentUri,null,contactIdKey + " = "+contactId,null,null);
        List<String> dataList = new ArrayList<>();
        while(phoneCursor != null && phoneCursor.moveToNext()){
            String phoneNum = phoneCursor.getString(phoneCursor.getColumnIndex(keyToGetData));
            dataList.add(phoneNum);
        }
        return dataList;
    }
}
